package memberrewards.dao;

import hackathon1212.EMF;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.Query;


import memberrewards.entities.BonusOffer;

public class BonusOfferDAO {

	private BonusOfferDAO(){		
	}
	
	public static ArrayList<BonusOffer> getEligibleBonusOffers(final String tier) {
		ArrayList<BonusOffer> bonusOffers=null;
		List<BonusOffer> tmpbonusOffers=getAllEligibleBonusOffers();
		if(tmpbonusOffers!=null){
			bonusOffers=new ArrayList<BonusOffer>();
			for (BonusOffer bonusOffer : tmpbonusOffers) {
				if(bonusOffer.getTierList().contains(tier)){
					bonusOffers.add(bonusOffer);
				}
			}
		}
		return bonusOffers;
	}
	
	@SuppressWarnings("unchecked")
	private static List<BonusOffer> getAllEligibleBonusOffers(){
		EntityManager entityManager=EMF.get().createEntityManager();
		List<BonusOffer> bonusOffers=null;
		try{
			Query query=entityManager.createQuery("SELECT bo FROM BonusOffer AS bo ");
			bonusOffers=query.getResultList();
		}
		finally{
			entityManager.close();
		}
		return bonusOffers;
	}

}
